package com.ckf.login_wx.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ckf.login_wx.entity.User;
import com.ckf.login_wx.servic.UserService;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import javax.servlet.http.HttpServletResponse;




@RestController
public class LoginController {

    /**
     * 登录
     * @param phone
     * @param password
     * @return
     */
	@Autowired
    private UserService userService;
	
	@RequestMapping("/doLogin")
    public Map<String, Object> doLogin(String phone, String password){
        Map<String, Object> map = new HashMap<>();
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        Map<String, Object> mapuser = new HashMap<>();
        
        mapuser.put("UserName", phone);
        mapuser.put("Password",password);
        queryWrapper.allEq(mapuser);
        User a=userService.getOne(queryWrapper);
        if (a!=null){
            map.put("code",200);
            map.put("truename",a.getTruename());
            map.put("username",a.getUsername());
            map.put("quxian",a.getQuxian());
            if(a.getQuxian().equals("0")){
                map.put("functions",shengchantou());
                map.put("functions2",shengchan());
                map.put("name","生产人员");
            }
            if(a.getQuxian().equals("1")){
                map.put("functions",zhidantou());
                map.put("functions2",zhidanxiaoshou());
                map.put("name","制单员");
            }
            if(a.getQuxian().equals("2")){
                map.put("functions",xiaoshoutou());
                map.put("functions2",zhidanxiaoshou());
                map.put("name","销售员");
            }
            if(a.getQuxian().equals("3")){
                map.put("functions",alltou());
                map.put("functions2",all());
                map.put("name","企业管理人员");
            }
            if(a.getQuxian().equals("4")){
                map.put("functions",alltou());
                map.put("functions2",all());
                map.put("name","系统管理员");
            }
            if(a.getQuxian().equals("5")){
                map.put("functions",kehutou());
                map.put("functions2",shengchan());
                map.put("name","客户");
            }
            map.put("result","登录成功");
            System.out.println("登录成功");
        }else {
            map.put("result","no");
            map.put("code",200);
        }
        return map;
    }
	@RequestMapping("/")
    public void jy(HttpServletResponse response) throws IOException{
		response.sendRedirect("login.html");
        //return "jy2021";
    }
	@RequestMapping("upload")
	public String uploadimg(MultipartFile file,String name) {
		System.out.println("***********上传图片**********"+name);
    	
		// 获取原始名字
		String fileName = file.getOriginalFilename();
		// 获取后缀名
		String suffixName = fileName.substring(fileName.lastIndexOf("."));
		// 文件保存路径
		String filePath = "c:/springboot_installl/img/";
		//String filePath = "C:/inetpub/makerting/Frm/Image/";
		// 文件重命名，防止重复
		String uuid=""+UUID.randomUUID();uuid=uuid.substring(2, 6);
		String img=uuid+fileName;
		//fileName = filePath +name+"-"+YY + "-" + M + "-" + D+ suffixName;
		fileName = filePath+name+suffixName;
		// 文件对象
		File dest = new File(fileName);
		// 判断路径是否存在，如果不存在则创建
		if(!dest.getParentFile().exists()) {
			dest.getParentFile().mkdirs();
		}
		try {
			// 保存到服务器中
			file.transferTo(dest);
			return img+"保存成功";
		} catch (Exception e) {
			e.printStackTrace();
			return img+"保存失败";
		}
	}
	@RequestMapping(value = "/gettu",produces = MediaType.IMAGE_JPEG_VALUE)
    @ResponseBody
    public byte[] getImage(String name) throws IOException {
		//System.out.println(name+"初始接收**********************************************************");
		name="Image/"+name.substring(6);
		//C:/springboot_installl/img/11.png
		name="C:/inetpub/makerting/Frm/"+name;
        File file = new File(name);
		FileInputStream inputStream = new FileInputStream(file);
        byte[] bytes = new byte[inputStream.available()];
        inputStream.read(bytes, 0, inputStream.available());
        return bytes;
    }
	//制单员头部权限
	public List<Map<String, String>> zhidantou() {
		String [] name   ={"原料制单","原料管理","货单制单","货单管理","库单制单","库单管理","货款制单","货款管理","支出","支出","收入","收入"};
		String [] pic_url={
				"../images/yuanliao.png",
				"../images/yuanliaoguanli.png",
				"../images/huodan.png",
				"../images/huodanguanli.png",
				"../images/kucun.png",
				"../images/kucunguanli.png",
				"../images/huokuan.png",
				"../images/huokuanguanli.png",
				"../images/zhichu2.png",
				"../images/zhichuguanli2.png",
				"../images/shouru2.png",
				"../images/shouruguanli2.png",
							};
		return index(name,pic_url);
	}
	//销售员头部权限
	public List<Map<String, String>> xiaoshoutou() {
		String [] name   ={"货单制单","货单管理","货款制单","原料","原料","库单","库单","货款","支出","支出","收入","收入"};
		String [] pic_url={
				"../images/huodan.png",
				"../images/huodanguanli.png",
				"../images/huokuan.png",
				"../images/yuanliao2.png",
				"../images/yuanliaoguanli2.png",
				"../images/kucun2.png",
				"../images/kucunguanli2.png",
				"../images/huokuanguanli2.png",
				"../images/zhichu2.png",
				"../images/zhichuguanli2.png",
				"../images/shouru2.png",
				"../images/shouruguanli2.png",
							};
		return index(name,pic_url);
	}
	//制单员销售员尾部权限
	public List<Map<String, String>> zhidanxiaoshou() {
		String [] name   ={"添加客户","修改密码","账号","账号","客户","库单","库单","库存","废单"};
		String [] pic_url={
				"../images/zhanghao.png",
				"../images/xuigaimima.png",
				"../images/zhanghaoguanli2.png",
				"../images/yonghu2.png",
				"../images/yonghuguanli2.png",
				"../images/yuandan2.png",
				"../images/yuandan2.png",
				"../images/kucun2.png",
				"../images/huodanguanli2.png",
							};
		return index(name,pic_url);
	}
	//管理员头部权限
	public List<Map<String, String>> alltou() {
		String [] name   ={"原料制单","原料管理","货单制单","货单管理","支出制单","支出管理","收入制单","收入管理","库单制单","库单管理","货款制单","货款管理"};
		String [] pic_url={
				"../images/yuanliao.png",
				"../images/yuanliaoguanli.png",
				"../images/huodan.png",
				"../images/huodanguanli.png",
				"../images/zhichu.png",
				"../images/zhichuguanli.png",
				"../images/shouru.png",
				"../images/shouruguanli.png",
				"../images/kucun.png",
				"../images/kucunguanli.png",
				"../images/huokuan.png",
				"../images/huokuanguanli.png",
							};
		return index(name,pic_url);
	}
	//管理员尾部权限
	public List<Map<String, String>> all() {
		String [] name   ={"账号管理","添加账号","添加客户","客户管理","型号规范","添加规范","库存剩余","修改密码","废单管理"};
		String [] pic_url={
				"../images/zhanghaoguanli.png",
				"../images/zhanghao.png",
				"../images/yonghu.png",
				"../images/yonghuguanli.png",
				"../images/yuandan.png",
				"../images/yuandan.png",
				"../images/kucun.png",
				"../images/xuigaimima.png",
				"../images/huodanguanli.png",
							};
		return index(name,pic_url);
	}
	//生产人员头部权限
	public List<Map<String, String>> shengchantou() {
		String [] name   ={"生产统计"};
		String [] pic_url={
				"../images/tongji3.png",
							};
		return index(name,pic_url);
	}
	//生产人员尾部权限
	public List<Map<String, String>> shengchan() {
		String [] name   ={"修改密码"};
		String [] pic_url={
				"../images/xuigaimima.png",
								};
		return index(name,pic_url);
	}

	//客户头部权限
	public List<Map<String, String>> kehutou() {
		String [] name   ={"售出统计"};
		String [] pic_url={
				"../images/tongji3.png",
							};
		return index(name,pic_url);
	}
	//首页
	public List<Map<String, String>> index(String [] name,String [] pic_url) {
		//String [] s ={};//"../images/原料管理.png"
        List<Map<String, String>> functions =new ArrayList<Map<String, String>>();
		for (int i = 0; i < name.length; i++) {
			Map<String, String> funmap = new HashMap<>();
	        funmap.put("name", name[i]);
	        funmap.put("pic_url", pic_url[i]);
	        functions.add(funmap);
		}
		
		
		
		return functions;

	}
	@RequestMapping("/indexinfo")
    public Object index(String usrname) throws IOException{
		
		User user= userService.list(new QueryWrapper<User>().eq("username", usrname)).get(0);
		if(user.getQuxian()=="4"){
			return admin();
		}
		if(user.getQuxian()=="2"){
			return xiaoshou();
		}
		if(user.getQuxian()=="1"){
			return zhidan();
		}
		if(user.getQuxian()=="0"){
			//map.put("quxian", "0");
		}
		if(user.getQuxian()=="5"){
			//map.put("quxian", "5");
		}
		return xiaoshou();
		
    }
	private Object admin() {
//		var infoname=["原料管理","货单管理","支出管理","收入管理","库存规范","库存管理","库存剩余","货款管理","客户管理","废单管理","账号管理","修改密码"];
//		var infourl=["mate.html","list2.html","exlist.html","incomelist.html","stockxh.html","stock.html","pay.html","shoper.html","shoper.html","sell2.html","user.html","password.html"];
//		var infoimg=["img/yuanliaoguanli.png","img/huodanguanli.png","img/zhichu.png","img/shouruguanli.png","img/yuandan.png","img/kucunguanli.png","img/kucun.png","img/huokuanguanli.png","img/yonghuguanli.png","img/zhanghaoguanli.png","img/huodanguanli.png","img/xuigaimima.png","img/123456.png"];
	    
		Map<String, Object> map = new HashMap<>();
		String [] pic_url={
				"mate.html",
				"list2.html",
				"exlist.html",
				"incomelist.html",
				"stockxh.html",
				"stock.html",
				"stocknum.html",
				"pay.html",
				"shoper.html",
				"sell2.html",
				"user.html",
				"password.html",
				};
		String [] pic_img={
	    		"img/yuanliaoguanli.png",
	    		"img/huodanguanli.png",
	    		"img/zhichu.png",
	    		"img/shouruguanli.png",
	    		
	    		"img/yuandan.png",
	    		"img/kucunguanli.png",
	    		"img/kucun.png",
	    		"img/huokuanguanli.png",
	    		
	    		"img/huodanguanli.png",
	    		"img/yonghuguanli.png",
	    		"img/zhanghaoguanli.png",
	    		"img/xuigaimima.png",
				};
		String [] pic_name={
				"原料管理","货单管理","支出管理","收入管理","库存规范","库存管理","库存剩余","货款管理","客户管理","废单管理","账号管理","修改密码"
				};
		map.put("img", pic_img);
		map.put("name", pic_name);
		map.put("url", pic_url);
		map.put("quxian", "4");
		return map;
	}
	private Object zhidan() {
		Map<String, Object> map = new HashMap<>();
		String [] pic_url={
				"mate.html",
				"list2.html",
				"false",
				"false",
				"false",
				"stock.html",
				"false",
				"pay.html",
				"addshoper.html",
				"false",
				"false",
				"password.html",
				};
		String [] pic_img={
	    		"img/yuanliaoguanli.png",
	    		"img/huodanguanli.png",
	    		"img/zhichu2.png",
	    		"img/shouruguanli2.png",
	    		
	    		"img/yuandan2.png",
	    		"img/kucunguanli.png",
	    		"img/kucun2.png",
	    		"img/huokuanguanli.png",
	    		
	    		"img/huodanguanli.png",
	    		"img/yonghuguanli2.png",
	    		"img/zhanghaoguanli2.png",
	    		"img/xuigaimima.png",
				};
		String [] pic_name={
				"原料管理","货单管理","支出管理","收入管理","库存规范","库存管理","库存剩余","货款管理","添加客户","废单管理","账号管理","修改密码"
				};
		map.put("img", pic_img);
		map.put("name", pic_name);
		map.put("url", pic_url);
		map.put("quxian", "1");
		return map;
	}
	private Object xiaoshou() {
		Map<String, Object> map = new HashMap<>();
		String [] pic_url={
				"mate.html",
				"false",
				"false",
				"false",
				"false",
				"false",
				"false",
				"addpay.html",
				"addshoper.html",
				"false",
				"false",
				"password.html",
				};
		String [] pic_img={
	    		"img/yuanliaoguanli2.png",
	    		"img/huodanguanli.png",
	    		"img/zhichu2.png",
	    		"img/shouruguanli2.png",
	    		
	    		"img/yuandan2.png",
	    		"img/kucunguanli2.png",
	    		"img/kucun2.png",
	    		"img/huokuanguanli.png",
	    		
	    		"img/huodanguanli.png",
	    		"img/yonghuguanli2.png",
	    		"img/zhanghaoguanli2.png",
	    		"img/xuigaimima.png",
				};
		String [] pic_name={
				"原料管理","货单管理","支出管理","收入管理","库存规范","库存管理","库存剩余","货款制单","添加客户","废单管理","账号管理","修改密码"
				};
		map.put("img", pic_img);
		map.put("name", pic_name);
		map.put("url", pic_url);
		map.put("quxian", "2");
		return map;
	}
}

//functions: [
//   {
//     "name": "原料制单",
//     "pic_url": '../images/原料.png'
//   },
//   {
//     "name": "原料管理",
//     "pic_url": '../images/原料管理.png'
//   },
//   {
//     "name": "货单制单",
//     "pic_url": '../images/货单.png'
//   },
//   {
//     "name": "货单管理",
//     "pic_url": '../images/货单管理.png'
//   },
//   {
//     "name": "支出制单",
//     "pic_url": '../images/支出.png'
//   },
//   {
//     "name": "支出管理",
//     "pic_url": '../images/支出管理.png'
//   },
//   {
//     "name": "收入制单",
//     "pic_url": '../images/收入.png'
//   },
//   {
//     "name": "收入管理",
//     "pic_url": '../images/收入管理.png'
//   },
//   {
//     "name": "库单制单",
//     "pic_url": '../images/库存.png'
//   },
//   {
//     "name": "库单管理",
//     "pic_url": '../images/库存管理.png'
//   },
//   {
//     "name": "货款制单",
//     "pic_url": '../images/货款.png'
//   },
//   {
//     "name": "货款管理",
//     "pic_url": '../images/货款管理.png'
//   }
// ],
// functions2: [
//   // {
//   //   "name": "原料统计",
//   //   "pic_url": '../images/原料统计.png'
//   // },
//   // {
//   //   "name": "货单统计",
//   //   "pic_url": '../images/货单统计.png'
//   // },
//   // {
//   //   "name": "库存统计",
//   //   "pic_url": '../images/库存统计.png'
//   // },
//   // {
//   //   "name": "欠款统计",
//   //   "pic_url": '../images/欠款统计.png'
//   // },
//   {
//     "name": "账号管理",
//     "pic_url": '../images/用户管理.png'
//   },
//   {
//     "name": "添加账号",
//     "pic_url": '../images/添加用户.png'
//   },
//   {
//     "name": "添加客户",
//     "pic_url": '../images/yonghu1.png'
//   },
//   {
//     "name": "客户管理",
//     "pic_url": '../images/yonghu.png'
//   },
//   {
//     "name": "库单型号",
//     "pic_url": '../images/yuanliao2.png'
//   },
//   {
//     "name": "库单规范",
//     "pic_url": '../images/yuanliao2.png'
//   },
//   {
//     "name": "库存剩余",
//     "pic_url": '../images/库存.png'
//   },
//   {
//     "name": "修改密码",
//     "pic_url": '../images/修改密码.png'
//   },
//   {
//     "name": "废单管理",
//     "pic_url": '../images/货单管理.png'
//   }
// ],
// functions3: [
//   {
//     "name": "修改密码",
//     "pic_url": '../images/修改密码.png'
//   }
// ]




